/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fullalbum.access;

import fullalbum.TransactionDAO;
import fullalbum.Utils;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author leoomoreira
 */
public class LoginServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        String login = request.getParameter("login");
        String password = request.getParameter("password");

        String sql = "select id, login, password, name from \"user\" where login = '" + login + "'";

        TransactionDAO dao = new TransactionDAO();

        ResultSet rs = dao.executeQuery(sql);

        dao.commit();
        dao.close();

        String name = null;
        String id = null;
        boolean loginAccepted = false;
        try {
            while (rs != null && rs.next()) {
                String queryUserId = rs.getString("id");
                String queryUserName = rs.getString("name");
                String queryLogin = rs.getString("name");
                String queryPassword = rs.getString("password");
                if (queryPassword != null && password != null && queryPassword.equals(Utils.encrypt(password))) {
                    loginAccepted = true;
                    id = queryUserId;
                    name = queryUserName;
                    break;
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

        request.setAttribute("login", login);
        if (loginAccepted) {
            HttpSession session = request.getSession(true);
            session.setAttribute("userId", id);
            session.setAttribute("userName", name);
            session.setAttribute("userLogin", login);
            RequestDispatcher dispatcher = request.getRequestDispatcher("MainServlet");
            dispatcher.forward(request, response);
        }
        else {
            request.setAttribute("message", "messages.login.unablelogin");
            RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
            dispatcher.forward(request, response);
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="Métodos HttpServlet. Clique no sinal de + à esquerda para editar o código.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
